package com.laikan.reader.database;

import org.greenrobot.greendao.query.DeleteQuery;
import org.greenrobot.greendao.query.QueryBuilder;
import thirdparty.greendao.database.DaoSession;
import thirdparty.greendao.database.HistoryDao;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by anrun on 8/10/16.
 */
public class DBHistory {

    private DaoSession daoSession;
    private HistoryDao historyDao;
    private static DBHistory dbHistory;

    private DBHistory() {
        daoSession = DBManager.getInstance().getDaoSession();
        historyDao = daoSession.getHistoryDao();
    }

    public static DBHistory getInstance() {
        if (dbHistory == null) {
            synchronized (DBHistory.class) {
                if (dbHistory == null) {
                    dbHistory = new DBHistory();
                }
            }
        }
        return dbHistory;
    }

    public void insertHistory(History history) {
        // 插入前判断该book是否已在数据库中
        if (queryById(history.getBookId()) != null) {
            update(history);
        }else {
            historyDao.insert(history);
        }
    }

    public void insetBookList(List<History> list) {
        if (list != null && !list.isEmpty()) {
            historyDao.insertInTx(list);
        }
    }

    public void deleteBookList(List<History> list) {
        if (list != null && !list.isEmpty()) {
            historyDao.deleteInTx(list);
        }
    }

    public void deleteHistory(History history){
        if (queryById(history.getBookId()) != null) {
            historyDao.delete(history);
        }
    }

    public History queryById(String id) {
        QueryBuilder<History> qb = historyDao.queryBuilder();
        qb.where(HistoryDao.Properties.BookId.eq(id));
        if (qb.list() == null || qb.list().size() == 0) {
            return null;
        }else {

            return qb.list().get(0);
        }
    }

    public List<History> queryAllHistory() {
        QueryBuilder<History> qb = historyDao.queryBuilder();
        return sortHistory(qb.list());
    }

    public void deleteById(String bookId) {
        QueryBuilder<History> qb = historyDao.queryBuilder();
        DeleteQuery<History> dq = qb.where(HistoryDao.Properties.BookId.eq(bookId)).buildDelete();
        dq.executeDeleteWithoutDetachingEntities();
    }

    public void clear() {
        historyDao.deleteAll();
    }

    public void update(History history) {
        QueryBuilder<History> qb = historyDao.queryBuilder();
        qb.where(HistoryDao.Properties.BookId.eq(history.getBookId()));
        List<History> list = qb.list();
        if (list != null && list.size() != 0) {
            historyDao.insertOrReplace(history);
        }
    }

    /**
     * 根据最后阅读时间对历史进行排序
     */
    private List<History> sortHistory(List<History> histories) {
        Collections.sort(histories, new Comparator<History>() {
            public int compare(History h1, History h2) {
                long diff = h1.getLastReadTime() - h2.getLastReadTime();
                if (diff < 0) {
                    return 1;
                }else if (diff == 0) {
                    return 0;
                }else {
                    return -1;
                }
            }
        });
        return histories;
    }
}
